<template>
	<div>
		<div class="h2">活动审批流程管理</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8" >
						<a-form-item label="维度" >
							<a-select allowClear optionFilterProp="children" placeholder="请先选择维度"
								v-decorator="[`weidu`]"
								@change="weiduChange" >
								<template v-for="(item, index) in weiduData">
									<a-select-option :key="item.ID">{{ item.Name }}</a-select-option>
								</template>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8" v-if="ClubTypeStatus">
						<a-form-item label="组织类型" >
							<a-select allowClear optionFilterProp="children" placeholder="请先选择学生组织类型"
								v-decorator="[`ClubTypeID`]"
								@change="selectChange" >
								<template v-for="(item, index) in ClubTypeList">
									<a-select-option :key="item.ID">{{ item.Name }}</a-select-option>
								</template>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8" v-if="ClubStatus">
						<a-form-item label="学生组织">
							<a-select :allowClear="true" v-decorator="['clubID']" placeholder="全部"
								optionFilterProp="children"  @change="selectChange2" showSearch @search="fetchClub"
				:filterOption="filterOption">
								<a-select-option v-for="(item, index) in ClubList" :key="index"
									:value="item.ID">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<!-- <ClubSearch :span="8" label="学生组织" :disabled="ClubStatus" name="clubID" ref="clubID" @tap="searchValueFun"></ClubSearch>
				 -->	<!-- <a-col :span="8">
						<a-form-item label="活动类型">
							<a-select :allowClear="true" v-decorator="['TypeID']" placeholder="全部"
								optionFilterProp="children" showSearch>
								<a-select-option v-for="(item, index) in ActivityTypeList" :key="index"
									:value="item.ID">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col> -->
					<a-col :span="8">
					<a-form-item label="课程类型">
						<a-tree-select :allowClear="true" show-search
							v-decorator="['activityTypeID']"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="ActivityTypeApplyList" tree-default-expand-all :replace-fields="replaceFields"
							placeholder="请先选择课程类型" @change="selectChangetree"></a-tree-select>
					</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="活动级别">
							<a-select :disabled="activeLeaveStatus" :allowClear="true" v-decorator="['activityLevelID']" placeholder="请先选择组织或者课程类型"
								optionFilterProp="children" showSearch @change="changelevel">
								<a-select-option v-for="(item, index) in LevelList" :key="index"
									:value="item.ID">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					
					<!-- <a-col :span="8" style="margin-top:4px;">
						<a-button type="primary" html-type="submit" class="btn1">搜索</a-button>
						<a-button :style="{ marginLeft: '8px' }" @click="handleReset" class="btn2">重置</a-button>
					</a-col> -->
				</a-row>
			</a-form>
		</div>
		<div class="option">
			<a-button type="primary" class="btn" @click="addList">新增</a-button>
		</div>
		<div>
			<el-table :data="data" v-loading="loading" border :max-height="tableHeight" highlight-current-row
				style="width: 100%;">
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="ClubTypeName" v-if="weiduType==2" label="学生组织类型" min-width="100">
					<template slot-scope="scope">
						{{ scope.row.ClubTypeName||'班级' }}
					</template>
				</el-table-column>
				<el-table-column prop="ClubName" label="学生组织" v-if="weiduType==1" min-width="100"></el-table-column>
				<el-table-column prop="ActivityTypeName" label="课程类型" min-width="100"></el-table-column>
				<el-table-column prop="ActivityLevelName" label="活动级别" min-width="100"></el-table-column>
				<el-table-column prop="FlowTypeName" label="审批节点" min-width="100"></el-table-column>
				<el-table-column label="操作" width="200" fixed="right">
					<template slot-scope="scope">
						<a-tooltip placement="top">
							<template slot="title">删除</template>
							<i title="删除" class="iconfont icon-delete" style="color: red;"
								@click="deleteList(scope.row)"></i>
						</a-tooltip>
						<a-tooltip placement="top" v-if="scope.$index != 0">
							<template slot="title">升序</template>
							<i title="升序" class="iconfont icon-paixu1" style="color: green;"
								@click="sortList(scope.row, 1)"></i>
						</a-tooltip>
						<a-tooltip placement="top" v-if="scope.$index != data.length - 1">
							<template slot="title">降序</template>
							<i title="降序" class="iconfont icon-paixu" style="color: brown;"
								@click="sortList(scope.row, 2)"></i>
						</a-tooltip>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<add :text="text" v-show="addIf" :activityLevelID="activityLevelID" :activityTypeID="activityTypeID" :ClubList="ClubList" :ClubTypeList="ClubTypeList" :clubID="clubID" :ClubTypeID="ClubTypeID"
			:ActivityFlowTypeList="currentActivityFlowTypeList" :ActivityTypeApplyList="ActivityTypeApplyList" :LevelList="LevelList" @closeFun="closeFun"></add>
	</div>
</template>

<script>
	import add from './add';
	import Func from '@/utils/func';
	import ClubSearch from '@/components/SearchForm/ClubSearch.vue'
	import {
		GetApplyActivityFlowListManage,
		GetActivityTypeList,
		DeleteApplyActivityFlow,
		DoSortApplyActivityFlow,
		DoGetActivityFlowTypeListCommon,
		GetClubTypeList,
		GetActivityLevelListCommon,
		GetClubPageListCommon
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add,
			ClubSearch
		},
		data() {
			return {
				activeLeaveStatus:true,
				weiduType:'',
				weiduData:[{
					ID:'1',
					Name:'学生组织'
				},
				{
					ID:'2',
					Name:'学生组织类型'
				}],
				ActivityTypeApplyList:[],
				replaceFields: {
					children: 'children',
					title: 'title',
					key: 'ID',
					value: 'ID'
				},
				ClubTypeStatus:false,
				ClubStatus:false,
				data,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				text: {},
				visible: false,
				confirmLoading: false,
				itemId: '',
				departmentList: [],
				worklist: [],
				loading: false,
				values: {},
				pages: {
					pageIndex: 1,
					pageSize: 20
				},
				tableHeight: parseFloat(window.innerHeight - 160),
				TeacherTypeList: this.global.TeacherTypeList,
				ActivityFlowTypeList: [],
				currentActivityFlowTypeList: [],
				ClubTypeList: [],
				ClubTypeID: '',
				ActivityTypeList: [],
				LevelList:[],
				ClubList:[],
				activityTypeID:"",
				clubID:'',
				activityLevelID:''
			};
		},
		computed: {},
		created() {
			// this.getList();
			this.getOtherList();
		},
		mounted() {},
		methods: {
			weiduChange(e){
				let that=this;
				that.weiduType=e
				if(e==1){
					this.ClubStatus=true;
					this.ClubTypeStatus=false;
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								clubID:''
							});
						}, 300);
					})
				}
				if(e==2){
					this.ClubTypeStatus=true;
					this.ClubStatus=false;
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ClubTypeID: ''
							});
						}, 300);
					})
				}
				this.$nextTick(() => {
					setTimeout(function() {
						that.form.setFieldsValue({
							activityTypeID:'',
							activityLevelID:''
						});
					}, 300);
				})
			},
			changelevel(e){
				this.activityLevelID=e
				this.getList()
			},
			selectChangetree(e){
				this.activityTypeID = e
				if(e!=undefined){
					this.activeLeaveStatus=false
				}else{
					this.activeLeaveStatus=true
				}
				
				this.getList();
			},
			filterOption(input, option) {
				return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
			},
			async fetchClub(value) {
				let myReg = /^[^a-zA-Z]+$/;
				if (value && value.length > 1) {
					let data = {};
					data.key = value;
					let res = await GetClubPageListCommon(data);
					this.ClubList = res.data.data;
				}
			},
			searchValueFun(name, value) {
				this.values = {
					...this.values,
					[name]: value
				}
				if(value!=''){
					this.ClubTypeStatus=true
				}else{
					this.ClubTypeStatus=false
				}
			},
			async getOtherList() {
				let res = await DoGetActivityFlowTypeListCommon();
				this.ActivityFlowTypeList = res.data.data;
				let res1 = await GetClubTypeList()
				let arr = [{
					ID: "Class",
					Name: "班级"
				}]
				this.ClubTypeList = arr.concat(res1.data.data)
				let res2 = await GetActivityTypeList(data);
				this.ActivityTypeList = res2.data.data
				let res4 = await GetActivityLevelListCommon()
				this.LevelList = res4.data.data
				
				this.ActivityTypeData = res2.data.data
				this.ActivityTypeApplyList= res2.data.data
				this.ActivityTypeApplyList = this.ActivityTypeApplyList.map(item => {
					return {
						...item,
						title: item.Name,
						// disabled: item.IsApply ? false : true
					};
				});
				this.ActivityTypeApplyList = this.toTree(this.ActivityTypeApplyList);
				console.log(this.ActivityTypeApplyList);

			},
			toTree(data) {
				let result = [];
				let map = {};
				if (!Array.isArray(data)) {
					return [];
				}
				data.forEach(item => {
					map[item.ID] = item;
				});
				data.forEach(item => {
					let parent = map[item.ParentID];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},
			selectChange(e) {
				console.log(e);
				
				this.ClubTypeID = e
				// if(e!=undefined){
				// 	this.ClubStatus=false
				// }else{
				// 	this.ClubStatus=true
				// }
				if(e!=undefined){
					this.activeLeaveStatus=false
					this.getList(e);
				}else{
					this.data=data
					this.activeLeaveStatus=true
				}
				
				
			},
			selectChange2(e){
				
				this.clubID=e
				if(e!=undefined){
					this.activeLeaveStatus=false
					this.getList(e);
				}else{
					this.data=data
					this.activeLeaveStatus=true
				}
				// this.getList()
				// if(e!=undefined){
				// 	this.ClubTypeStatus=false
				// }else{
				// 	this.ClubTypeStatus=true
				// }
			},
			handleSearch(e) {
				this.form.validateFields((error, values) => {
					if (!error) {
						this.pagination.currentPage = 1;
						this.values = values;
						this.getList();
					}
				});
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList(id) {
				this.loading = true;
				let data = {};
				data.ClubTypeID = this.ClubTypeID
				data.activityTypeID=this.activityTypeID
				data.clubID=this.clubID
				data.activityLevelID=this.activityLevelID
				let res = await GetApplyActivityFlowListManage(data);
				this.loading = false;
				this.data = res.data.data;
			},
			addList() {
				this.form.validateFields((error, values) => {
					if (!error) {
						this.values = values;
						console.log(this.values);
						this.text = {};
						this.ClubTypeID = values.ClubTypeID
						this.clubID = values.clubID
						this.activityLevelID = values.activityLevelID
						this.activityTypeID = values.activityTypeID
						if (this.ClubTypeID == 'Class') {
							this.currentActivityFlowTypeList = this.ActivityFlowTypeList.filter((m, i) => m
								.IsTeacherExam == false)
						} else {
							this.currentActivityFlowTypeList = this.ActivityFlowTypeList
						}
						this.addIf = !this.addIf;
					}
				});
			},
			modifyList(text) {
				text = {
					...text,
					time: new Date()
				}
				this.addIf = !this.addIf;
				this.text = text;
			},
			deleteList(text) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定删除该审批节点吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await DeleteApplyActivityFlow(data);
						if (res.data.code == 0) {
							that.getList(that.ClubTypeID);
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			async sortList(text, type) {
				// 1=向上 2=向下
				let data = {};
				data.id = text.ID;
				data.type = type;
				let res = await DoSortApplyActivityFlow(data);
				if (res.data.code == 0) {
					this.getList(this.ClubTypeID);
					this.$message.success(res.data.msg);
				} else {
					this.$message.error(res.data.msg);
				}
			},
			closeFun(data) {
				this.addIf = false;
				if (data == 1) {
					this.getList(this.ClubTypeID);
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	.home-form /deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	.home-form /deep/.ant-form-item-label {
		width: 110px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	.home-form /deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.el-table th.is-leaf {
		background: #fafafa;
	}

	/deep/.el-table td,
	/deep/.el-table th {
		padding: 5px;
	}

	.option {
		margin-top: 10px;
		margin-bottom: 20px;
		overflow: hidden;
	}

	.option .btn {
		height: 36px;
		line-height: 36px;
		padding: 0 10px;
		float: left;
		background: #1890ff;
		color: #fff;
		margin-right: 10px;
		border-radius: 3px;
		cursor: pointer;
		border: none;
	}

	.foot {
		text-align: center;
		margin-top: 100px;
	}

	.el-pagination {
		margin-top: 10px;
	}

	/deep/ .ant-form-item-control-wrapper {
		min-width: 200px;
	}
</style>
